#!/usr/bin/env python
#

"""Utility to quickly deploy portable apps to all computers in LAN.

This version works on a windows machine only.
"""

__version__ = "$Revision$"
# Last Modified on $Date$
# by $Author$
# from $Source$

import os
import os.path
import shutil
import logging
import sys
import string
import glob
import subprocess

from mutils import fdelete, log

# All computers in LAN where you want to deploy your apps.
dest_comps = [ "\\\\MCA-78\\",
               "\\\\MCA-1\\",
               "\\\\MCA-10\\",
               "\\\\MCA-100\\",
               "\\\\MCA-11\\",
               "\\\\MCA-13\\",
               "\\\\MCA-14\\",
               "\\\\MCA-15\\",
               "\\\\MCA-16\\",
               "\\\\MCA-17\\",
               "\\\\MCA-18\\",
               "\\\\MCA-19\\",
               "\\\\MCA-21\\",
               "\\\\MCA-22\\",
               "\\\\MCA-23\\",
               "\\\\MCA-24\\",
               "\\\\MCA-25\\",
               "\\\\MCA-26\\",
               "\\\\MCA-3\\",
               "\\\\MCA-31\\",
               "\\\\MCA-32\\",
               "\\\\MCA-34\\",
               "\\\\MCA-35\\",
               "\\\\MCA-38\\",
               "\\\\MCA-39\\",
               "\\\\MCA-40\\",
               "\\\\MCA-42\\",
               "\\\\MCA-49\\",
               "\\\\MCA-54\\",
               "\\\\MCA-57\\",
               "\\\\MCA-7\\",
               "\\\\MCA-78\\",
               "\\\\MCA-8\\",
               "\\\\MCA150\\",
               "\\\\MCA41\\",
             ]


#The entries in this list should not end with slash.
src_dirs = [ "C:\\FirefoxPortable",
             "C:\\7-ZipPortable",
             "C:\\SumatraPDFPortable",
             "C:\\CodeBlocks",
           ]

src_files = [ "C:\\Documents and Settings\\All Users\\Desktop\\Firefox Web Browser.lnk",
              "C:\\Documents and Settings\\All Users\\Desktop\\CodeBlocks C IDE.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Firefox Web Browser.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\CodeBlocks C IDE.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\7-Zip.lnk",
              "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Sumatra PDF.lnk",
            ]


del_search_list = [ "*codeblocks*",
                    "*firefox*",
                  ]
del_search_dirs = [ "C:\\Program Files\\",
                    "C:\\Documents and Settings\\*\\Desktop\\",
                    "C:\\Documents and Settings\\*\\Start Menu\\Programs\\",
                    "C:\\Documents and Settings\\*\\Application Data\\Microsoft\\Internet Explorer\\Quick Launch\\",
                    "C:\\Documents and Settings\\*\\Application Data\\",
                  ]

xcopy_cmd = 'XCOPY /D /E /R /Y /V /C /Q /H /K /I'

def delete_old_data(dest_comp):
    log.debug('Deleting old data')
    for a in del_search_list:
        for b in del_search_dirs:
            for c in glob.glob( dest_comp + b.replace('C:','C$') + a ):
                try:
                    fdelete(c)
                    #log.debug('Will delete' + c)
                except:
                    log.error( 'Unexpected error while deleting %s : %s' % (c, sys.exc_info()[1]) )


def deploy(dest_comp):

    for src_dir in src_dirs:
        try:
            if not os.path.exists(src_dir):
                log.critical('Source dir %s does not exist' % src_dir)
                sys.exit(1)

            log.info('Deploying ' + src_dir)
            dest_dir = os.path.join(dest_comp, src_dir.replace('C:','C$'))

            #The following operation is slow for big directories.
            #if os.path.exists(dest_dir):
            #    shutil.rmtree(dest_dir)
            #shutil.copytree(src_dir, dest_dir)

            #Using window's xcopy.
            #I know, it's ugly.
            retcode = subprocess.call('%s "%s" "%s"' % (xcopy_cmd, src_dir, dest_dir), shell=True)
            if retcode:
                log.error('xcopy command returned error while copying "%s" to "%s"' % (src_dir, dest_dir))

        except:
            log.error( 'Unexpected error while deploying %s : %s ' % (src_dir, sys.exc_info()[1]) )

def create_shortcuts(dest_comp):

    try:

        log.debug('Creating shortcuts')
        for src_file in src_files:

            if not os.path.exists(src_file):
                log.critical('Source file %s does not exist' % src_file)
                sys.exit(1)

            dest_file = os.path.join(dest_comp, src_file.replace('C:','C$'))
            log.debug('Creating file %s' % dest_file)

            dir_name = os.path.dirname(dest_file)

            if not os.path.exists(dir_name):
                log.debug('Creating dir %s' % dir_name)
                os.makedirs(dir_name)

            shutil.copy2(src_file, dest_file)
    except:
        log.error( 'Unexpected error: %s' % sys.exc_info()[1] )


def main():

    for dest_comp in dest_comps:
        log.info('-----------------------------------------------------')

        if not os.path.exists(dest_comp + 'C$'):
            log.info('Can not reach computer %s' % dest_comp)
            continue

        log.info('Started deploying to computer ' + dest_comp)

        delete_old_data(dest_comp)
        deploy(dest_comp)
        create_shortcuts(dest_comp)

        log.info('Finished deploying to computer ' + dest_comp)



if __name__ == '__main__':
    sys.exit(main())
